package dom4j;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

public class DOM4JWorkDemo {
    public static void main(String[] args) throws Exception {
        //1.利用SaxReader读取一个xml文件
        Document document = new SAXReader().read("day01-xml-json/HomeWork.xml");
//        System.out.println(document);

        //2.利用Document对象获取根节点
        Element rootElement = document.getRootElement();
        System.out.println("Element Name:" + rootElement.getName());

        //3.获取根节点下的所有子节点
        List<Element> elements = rootElement.elements();

        //4.遍历所有子节点，进一步获取子节点中的内容
        for (Element childElement : elements) {
            System.out.println("  Element Name:"+ childElement.getName());

            //5.获取当前遍历的product元素中的子元素
            for (Element ele : childElement.elements()) {
                System.out.println("   Element Name:"+ele.getName() + "\n" +
                                "   Element Content:"+ ele.getText());
            }
        }
    }
}
